package com.zhaoyh.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.zhaoyh.bean.Userincome;
import com.zhaoyh.bean.Userinfo;
import com.zhaoyh.framework.code.model.PageBean;
import com.zhaoyh.framework.code.util.LogUtil;
import com.zhaoyh.result.UserincomeResult;
import com.zhaoyh.service.UserincomeService;
import com.zhaoyh.service.UserinfoService;
import com.zhaoyh.util.MathUtils;
import com.zhaoyh.vo.UserincomeVO;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 祥霸
 * @date 2019/2/22
 */
@Controller
@RequestMapping(value = "/userincome")
public class UserincomeController extends BaseController{

    @Autowired
    UserincomeService userincomeService;

    @Autowired
    UserinfoService userinfoService;

    @RequestMapping(value  = "/ajaxList", method = RequestMethod.POST)
    public void ajaxList(@RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo, @RequestParam(value = "topCondition", defaultValue = "")String account, HttpServletResponse response) {

        LogUtil.log.info("/userincome/ajaxList" + account);
        List<Userincome> departments = new ArrayList<Userincome>();
        try {
            String uuid = getUuid(userinfoService, account);

            //每页显示的记录行数
            int limit = 20000;
            //总记录数
            int totalItems = userincomeService.getCount(uuid);
            int temp = totalItems / limit;
            int totalPages = (totalItems % limit== 0) ? temp : temp+1;
            //每页的起始行(offset+1)数据，如第一页(offset=0，从第1(offset+1)行数据开始)
            int offset = (pageNo - 1)*limit;
            if (totalItems > 0) {
                departments = userincomeService.listUserincome(offset, limit, uuid);
            }

            UserincomeResult resule = new UserincomeResult();

            if (departments != null) {
                for (Userincome income: departments) {
                    UserincomeVO incomeChartsVO = new UserincomeVO();
                    incomeChartsVO.setValue(income.getMoney());
                    incomeChartsVO.setFullDate(DateFormatUtils.format(income.getDatatime(), "yyyy-MM-dd HH:mm:ss"));
                    incomeChartsVO.setEntityID(income.getEntityid());
                    resule.getList().add(incomeChartsVO);
                }
            }

            resule.setCurPageNo(pageNo);
            resule.setTotalPages(totalPages);
            resule.setTotalItems(totalItems);

            String jsonResult = JSON.toJSONString(resule);
            renderData(response, jsonResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/editorList")
    public ModelAndView getEditorList(@RequestParam(value = "topCondition", defaultValue = "")String account) {
        ModelAndView mv = new ModelAndView("console/userincomeEditor");
        try {
            String uuid = this.getUuid(userinfoService, account);

            //每页显示的记录行数
            int limit = 20000;

            //每页的起始行(offset+1)数据，如第一页(offset=0，从第1(offset+1)行数据开始)
            int offset = 0;
            List<Userincome> departments = userincomeService.listUserincome(offset, limit, uuid);

            List<String> date = new ArrayList<String>();
            List<Double> data = new ArrayList<Double>();
            if (departments != null) {
                for (Userincome income: departments) {
                    date.add(DateFormatUtils.format(income.getDatatime(), "yyyy-MM-dd HH:mm:ss"));
                    data.add(income.getMoney());
                }
            } else {
                date.add("2011-11-11");
                data.add(1.0);
            }
            mv.addObject("date", date).addObject("data", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    @RequestMapping(value  = "/list")
    public ModelAndView list(@RequestParam(value = "pageNumber", defaultValue = "1")Integer pageNumber, @RequestParam(value = "topCondition", defaultValue = "")String account) {
        ModelAndView mv = new ModelAndView("console/listUserincome");
        List<Userincome> departments = new ArrayList<Userincome>();
        try {
            String uuid = userinfoService.getUuid(account);

            if (StringUtils.isEmpty(uuid)) return mv;

            //每页显示的记录行数
            int limit = 200;
            PageBean pageBean = new PageBean(limit);

            //总记录数
            int totalItems = userincomeService.getCount(uuid);

            if (limit * pageNumber > totalItems) {
                pageNumber = MathUtils.ceil(totalItems, limit);
            }
            pageBean.setDataSum(totalItems);
            pageBean.setPageNumber(pageNumber);

            //每页的起始行(offset+1)数据，如第一页(offset=0，从第1(offset+1)行数据开始)
            int offset = (pageNumber - 1)*limit;

            if (totalItems > 0) {
                departments = userincomeService.listUserincome(offset, limit, uuid);
            }

            mv.addObject("departments", departments).addObject("pageBean", pageBean).addObject("topCondition", account);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
}
